/*****************************************************
Copyright (C) 2020. All rights reserved.
File name     ：TestMain.c
Version       ：v1.0
Author        ：zhengqijun
Date          ：2020-04-04
Function List ：
Description   ：main function
******************************************************/

#include <stdio.h>  // for printf
#include "BinaryHeap.h"

#define MaxSize (50)

// PrintBinaryHeap for testing
static void PrintDepth(ElementType X, int depth);
static void PrintBinaryHeap(PriorityQueue H, int depth, int Index);

void PrintDepth(ElementType X, int depth) {
    while (depth > 0) {
        printf("    ");
        --depth;
    }
    printf("%02d\n", X);
}

void PrintBinaryHeap(PriorityQueue H, int depth, int Index) {
    if (Index <= Size(H)) {
        PrintBinaryHeap(H, depth + 1, 2 * Index + 1);
        PrintDepth(Retrieve(H, Index), depth);
        PrintBinaryHeap(H, depth + 1, 2 * Index);
    }
}

// main function
int main(void) {
    PriorityQueue H;
    PriorityQueue H2;
    ElementType A[9];
    int i;
    int j;

    H = Initialize(MaxSize);

    if (H == NULL) {
        printf("PriorityQueue is empty!\n");
    } else {
        printf("PriorityQueue is not empty!\n");
    }

    printf("Insert priority queue: [ ");
    for (i = 1, j = MaxSize / 2; i < MaxSize; i += 3) {
        printf("%02d ", j);
        Insert(j, H);
        j = (j + 21) % MaxSize;
    }
    printf("]\n");

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    printf("Find min element is %d in priority queue!\n", FindMin(H));
    printf("Delete min element is %d in priority queue!\n", DeleteMin(H));

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    printf("Increase 2nd key by 60!\n");
    IncreaseKey(2, 60, H);

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    printf("Decrease 6nd key by 20!\n");
    DecreaseKey(6, 20, H);

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    printf("Delete 5nd key!\n");
    Delete(5, H);

    printf("Print priority queue:\n");
    PrintBinaryHeap(H, 0, 1);
    printf("\n");

    printf("Destroy priority queue\n");
    Destroy(H);

    printf("Insert priority queue: [ ");
    for (i = 0; i < 9; ++i) {
        A[i] = 9 - i;
        printf("%d ", A[i]);
    }
    printf("]\n");

    H2 = BuildHeap(A, 9);

    printf("Print priority queue:\n");
    PrintBinaryHeap(H2, 0, 1);
    printf("\n");

    printf("Destroy priority queue\n");
    Destroy(H2);

    return 0;
}

// EOF

